home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / game / patch / civman.lha / CivMan / CivManager13.e < prev    next >
Encoding:
Text File  |  1992-02-26  |  10.6 KB  |  434 lines

  1. /* Monitors a civilisation save in RAM:, and copies it to it's home
  2.    path when it is saved by civ. */
  3.  
  4. /* Version 1.1 changes:
  5.  
  6.  * Now reads initial directory from tooltypes
  7.  
  8.  * Makes civassigns from tooltypes (hackily by executing c:ASSIGN !)
  9.  
  10.  * No need for a script
  11.  
  12.  * First shift selected, or toolmanager dropped file is used as
  13.  
  14.    the game save file!
  15.  
  16.  * File includes a version string
  17.  
  18. */
  19.  
  20. /* Version 1.2 Changes 
  21.  
  22.  * Now a commodity - Can be quit/supsended for CX_Exchange
  23.  
  24.  * Gives Option To Delete the files on exit
  25.  
  26.  * Compatabile with version command
  27.  
  28.  * Creates Icons For Saved Files
  29.  
  30. */
  31.  
  32. /* Version 1.3 Changes
  33.  
  34.  * Now creates assigns internally without executing C:Assign
  35.  
  36. */
  37.  
  38. OPT OSVERSION=37
  39.  
  40. MODULE 'ReqTools','libraries/reqtools','utility/tagitem','Dos/Dos',
  41.        'dos/notify','workbench/startup','workbench/workbench','icon',
  42.        'commodities','libraries/commodities','exec/ports'
  43.  
  44.  
  45. CONST SVEFILESIZE=37869
  46. CONST MAPFILESIZE=64000
  47. CONST RW_ERR=-1
  48.  
  49.  
  50. ENUM ER_NONE,ER_OPEN,ER_READ,ER_WRITE,ER_NOSIG,ER_MEM,ER_NONOTIFY,ER_CIVLOAD,
  51.      ER_OPENLIB,ER_MSGPORT,ER_BROKER,ER_ASSIGN,ER_FONT,ER_LOCK
  52.  
  53. RAISE ER_OPEN IF Open()=NIL
  54. RAISE ER_READ IF Read()=RW_ERR
  55. RAISE ER_WRITE IF Write()=RW_ERR
  56. RAISE ER_MEM IF New()=NIL
  57. RAISE ER_NOSIG IF AllocSignal()=RW_ERR
  58. RAISE ER_NONOTIFY IF StartNotify()=0
  59. RAISE ER_CIVLOAD IF Execute()=0
  60. RAISE ER_OPENLIB IF OpenLibrary()=0
  61. RAISE ER_MSGPORT IF CreateMsgPort()=0
  62. RAISE ER_BROKER IF CxBroker()=0
  63. RAISE ER_ASSIGN IF AssignLock()=0
  64. RAISE ER_FONT IF AssignAdd()=0
  65. RAISE ER_LOCK IF Lock()=0
  66.  
  67. DEF quitstring[30]:STRING
  68. DEF filename[256]:STRING
  69. DEF defdir[256]:STRING
  70. DEF civdir[256]:STRING
  71. DEF fontdir[256]:STRING
  72.  
  73. /* Commodity Stuff */
  74.  
  75. DEF broker:PTR TO newbroker
  76. DEF messport:PTR TO mp
  77. DEF brokerobj=0,cxsigbit
  78.  
  79.  
  80. PROC main() HANDLE
  81.  
  82.   DEF iconfile,iconbuf,buflen=0        /* Icon IO */
  83.   DEF iconname[256]:STRING
  84.   DEF quitprog,sig,active        /* program */
  85.   DEF msg,msgtype,msgid            /* commodity */
  86.   DEF notifysig,notifysigbit,task    /* notify */
  87.   DEF nreq:PTR TO notifyrequest
  88.   DEF lck
  89.  
  90.   /* Set the default AMOS_System path if no arguments */
  91.  
  92.   quitstring := 'Quit CivMangaer'
  93.   cxbase := OpenLibrary('commodities.library',37)
  94.  
  95.   /* Initiailize as a commodity */
  96.  
  97.   broker:=[NB_VERSION,0,'CivManager','Civilization Game-Save Manager V1.2','©1994 Paul Hickman (ph@doc.ic.ac.uk)',
  98.            NBU_UNIQUE OR NBU_NOTIFY,0,0,0,0]:newbroker
  99.   messport := CreateMsgPort()
  100.   broker.port := messport
  101.   brokerobj := CxBroker(broker,0)
  102.   cxsigbit := Shl(1,messport.sigbit)
  103.   ActivateCxObj(brokerobj,1)
  104.   
  105.   /* Locate File */
  106.  
  107.   getwbargs()
  108.  
  109.   /* Make civilisations assigns for it */
  110.  
  111.   lck := Lock(civdir,ACCESS_READ)
  112.   AssignLock('CIV1',lck)
  113.   lck := Lock(civdir,ACCESS_READ)
  114.   AssignLock('CIV2',lck)
  115.   lck := Lock(civdir,ACCESS_READ)
  116.   AssignLock('CIV3',lck)
  117.   lck := Lock(civdir,ACCESS_READ)
  118.   AssignLock('CIV4',lck)
  119.  
  120.   IF fontdir=''
  121.     StrCopy(fontdir,civdir,ALL)
  122.     StrAdd(fontdir,'/fonts',ALL)
  123.   ENDIF
  124.  
  125.   lck := Lock(fontdir,ACCESS_READ)
  126.   AssignAdd('FONTS',lck) 
  127.  
  128.   /* Now load file, if it exists */
  129.  
  130.   IF FileLength(filename) <> -1
  131.     IF FileLength(filename) = 101869
  132.       copytoram(filename)
  133.     ELSE
  134.       request('\s is not a CivManager Save File',quitstring,NIL)
  135.       CleanUp(20)
  136.     ENDIF
  137.   ENDIF
  138.  
  139.   /* Load Icon File */
  140.  
  141.   StrCopy(iconname,civdir,ALL)
  142.   StrAdd(iconname,'/DefSaveIcon.info',ALL)
  143.   IF (buflen := FileLength(iconname)) <> -1
  144.     iconfile := Open(iconname,MODE_OLDFILE)
  145.     iconbuf := New(buflen)
  146.     Read(iconfile,iconbuf,buflen)
  147.     Close(iconfile)
  148.   ENDIF
  149.   StrCopy(iconname,filename,ALL)
  150.   StrAdd(iconname,'.info',ALL)
  151.  
  152.   /* Setup notifier */
  153.  
  154.   nreq:=New(SIZEOF notifyrequest)     /* memory is cleared */
  155.   notifysigbit:=AllocSignal(-1)       /* we want to be signalled */
  156.   task:=FindTask(0)
  157.   nreq.name:='RAM:CIVIL0.SVE'         /* fill in structure */
  158.   nreq.flags:=NRF_SEND_SIGNAL
  159.   nreq.port:=task                     /* union port/task */
  160.   nreq.signalnum:=notifysigbit
  161.  
  162.   notifysig:=Shl(1,notifysigbit)
  163.   active:=TRUE
  164.   StartNotify(nreq)
  165.  
  166.   /* Load Civilization */
  167.  
  168.   Execute('run CIV1:CivilizationAGA',NIL,NIL)
  169.  
  170.   /* Monitor File Loop */
  171.  
  172.   REPEAT
  173.  
  174.     /* Wait for civilisation to    save the game, or a commodities
  175.        signal to occur */
  176.  
  177.     sig := Wait(notifysig OR cxsigbit OR SIGBREAKF_CTRL_C)
  178.  
  179.     IF (sig AND notifysig)
  180.       /* Signal was a file modification */
  181.       IF active
  182.         Delay(50)         /* Wait 1 second to ensure writing
  183.                 is completed from civilization */
  184.         IF (quitprog := FileLength('RAM:CIVIL0.SVE')) <> -1 
  185.           copyfromram(filename)
  186.           IF buflen
  187.             /* If the default icon was successfully read */
  188.             iconfile := Open(iconname,MODE_NEWFILE)
  189.             Write(iconfile,iconbuf,buflen)
  190.             Close(iconfile)
  191.           ENDIF
  192.         ENDIF
  193.       ENDIF
  194.     ELSE
  195.       /* Signal was from commodities */
  196.       WHILE msg := GetMsg(messport)
  197.         msgtype:=CxMsgType(msg)
  198.         msgid:=CxMsgID(msg)
  199.         ReplyMsg(msg)
  200.         IF msgtype = CXM_COMMAND
  201.           SELECT msgid
  202.             CASE CXCMD_DISABLE   
  203.               active:=FALSE
  204.               ActivateCxObj(brokerobj,0)
  205.             CASE  CXCMD_ENABLE
  206.               active:=TRUE
  207.               ActivateCxObj(brokerobj,1)
  208.             CASE CXCMD_KILL
  209.               quitprog:=-1
  210.             CASE CXCMD_UNIQUE
  211.               quitprog:=-1
  212.             ENDSELECT
  213.         ENDIF
  214.       ENDWHILE
  215.       IF (sig AND SIGBREAKF_CTRL_C)
  216.         quitprog := -1
  217.       ENDIF
  218.     ENDIF
  219.   UNTIL quitprog = -1
  220.  
  221.   FreeSignal(notifysigbit)
  222.   DeleteCxObjAll(brokerobj)
  223.   WHILE (msg:=GetMsg(messport))
  224.     ReplyMsg(msg)
  225.   ENDWHILE
  226.   DeleteMsgPort(messport)
  227.  
  228.   IF request('Thank you for using CivManager V1.3\nBy Paul Hickman (ph@doc.ic.ac.uk)\n\nShall I delete the files in RAM:\nBefore Quitting?','  Yes  |  No  ',NIL)
  229.  
  230.     DeleteFile('RAM:CIVIL0.SVE')
  231.     DeleteFile('RAM:CIVIL0.MAP')
  232.   
  233.   ENDIF
  234.  
  235. EXCEPT
  236.  
  237. IF sig<>-1 THEN FreeSignal(notifysigbit)
  238.  
  239. IF brokerobj
  240.   DeleteCxObjAll(brokerobj)
  241.   WHILE (msg:=GetMsg(messport))
  242.     ReplyMsg(msg)
  243.   ENDWHILE
  244.   DeleteMsgPort(messport)
  245. ENDIF
  246.  
  247. SELECT exception
  248.   CASE ER_OPEN;      request('Error: Could Not Open A File',quitstring,NIL)
  249.   CASE ER_READ;      request('Error: Could Not Read A File',quitstring,NIL)
  250.   CASE ER_WRITE;     request('Error: Could Not Write To A File',quitstring,NIL)
  251.   CASE ER_MEM;       request('Error: Out Of Memory Error',quitstring,NIL)
  252.   CASE ER_NOSIG;     request('Error: Could Not Allocate Signal Bit',quitstring,NIL)
  253.   CASE ER_NONOTIFY;  request('Error: Cannot watch \aRAM:CIVIL0.SVE\a',quitstring,NIL)
  254.   CASE ER_CIVLOAD;   request('Error: Could Not Load Civilization',quitstring,NIL)
  255.   CASE ER_OPENLIB;   request('Error: Could Not Open Required Libraries',quitstring,NIL) 
  256.   CASE ER_MSGPORT;   request('Error: Could Not Create A Message Port',quitstring,NIL)
  257.   CASE ER_BROKER;    /* Not an error - this is "run twice" quitting the 2nd copy */
  258.   CASE ER_LOCK;      request('Error: Could Not Lock Directory',quitstring,NIL)
  259.   CASE ER_ASSIGN;    request('Error: Could Not Assign CIV?:',quitstring,NIL)
  260.   CASE ER_FONT;      request('Error: Could Not Assign FONTS:',quitstring,NIL)
  261.   DEFAULT;           request('Error: An IO Error Has Occured',quitstring,NIL)
  262. ENDSELECT
  263.  
  264. ENDPROC
  265.  
  266. PROC copytoram(fromname) HANDLE
  267.   /* Copy the file */
  268.  
  269.   DEF buffer,infile,outfile
  270.  
  271.   infile := Open(fromname,MODE_OLDFILE)
  272.   outfile := Open('ram:CIVIL0.map',MODE_NEWFILE)
  273.   buffer := New(MAPFILESIZE)
  274.   
  275.   Read(infile,buffer,MAPFILESIZE)
  276.   Write(outfile,buffer,MAPFILESIZE)
  277.   Close(outfile)
  278.   
  279.   outfile := Open('ram:CIVIL0.sve',MODE_NEWFILE)
  280.   Read(infile,buffer,SVEFILESIZE) 
  281.   Write(outfile,buffer,SVEFILESIZE) 
  282.   Close(outfile)
  283.  
  284.   Close(infile)
  285.   Dispose(buffer)
  286.  
  287. EXCEPT
  288.   
  289.  Raise(exception)
  290.  
  291. ENDPROC
  292.  
  293.  
  294. PROC copyfromram(toname) HANDLE
  295.   /* Copy the file */
  296.  
  297.   DEF buffer,infile,outfile
  298.  
  299.   outfile := Open(toname,MODE_NEWFILE)
  300.   infile := Open('ram:CIVIL0.map',MODE_OLDFILE)
  301.   buffer := New(MAPFILESIZE)
  302.   
  303.   Read(infile,buffer,MAPFILESIZE)
  304.   Write(outfile,buffer,MAPFILESIZE)
  305.   Close(infile)
  306.  
  307.   infile := Open('ram:CIVIL0.sve',MODE_OLDFILE)
  308.   Read(infile,buffer,SVEFILESIZE)
  309.   Write(outfile,buffer,SVEFILESIZE)
  310.   Close(outfile)
  311.  
  312.   Close(infile)
  313.   Dispose(buffer)
  314.  
  315. EXCEPT
  316.  
  317. Raise(exception)
  318.  
  319. ENDPROC
  320.  
  321.  
  322. PROC filereq(defdir,dirbuf) HANDLE
  323.  
  324.   CONST FILEREQ=0,REQINFO=1
  325.  
  326.   DEF filebuf[120]:STRING
  327.   DEF req:PTR TO rtfilerequester
  328.   DEF tempstr[1]:STRING
  329.  
  330.   reqtoolsbase:=OpenLibrary('reqtools.library',37)
  331.  
  332.   /* Setup the requester */
  333.  
  334.   IF req:=RtAllocRequestA(FILEREQ,0)
  335.     filebuf := 'Save.Civil'
  336.     RtChangeReqAttrA(req,[RTFI_DIR,defdir,RTFI_MATCHPAT,'#?.Civil',TAG_DONE])
  337.     RtFileRequestA(req,filebuf,'Select A CivManger Game File',[RTFI_FLAGS,FREQF_PATGAD,TAG_DONE])
  338.  
  339.     /* combine the directory & filename */
  340.  
  341.     StrCopy(dirbuf,req.dir,ALL)
  342.     RtFreeRequest(req)
  343.     RightStr(tempstr,dirbuf,1)
  344.     IF StrCmp(tempstr,':',1)=FALSE THEN StrAdd(dirbuf,'/',ALL)
  345.     StrAdd(dirbuf,filebuf,ALL)
  346.  
  347.   ENDIF
  348.  
  349.   CloseLibrary(reqtoolsbase)
  350.  
  351.  
  352. EXCEPT
  353.  
  354. Raise(exception)
  355.  
  356. ENDPROC 
  357.  
  358. PROC request(body,gadgets,args)
  359. ENDPROC EasyRequestArgs(0,[20,0,'Civilization Manager V1.3',body,gadgets],0,args)
  360.  
  361. PROC getwbargs() HANDLE
  362.  
  363.   /* Variables for reading tooltypes & arguments */
  364.  
  365.   DEF wb:PTR TO wbstartup
  366.   DEF args:PTR TO wbarg
  367.   DEF dobj:PTR TO diskobject
  368.   DEF typtr,toolarray
  369.   DEF olddir=NIL
  370.  
  371.   /* Variables to hold results */
  372.  
  373.  
  374.   IF wbmessage=NIL                /* we got from cli */
  375.     WriteF('Civmanger V1.3 must be run from the workbench.\n\n')
  376.   ELSE                        /* from wb */
  377.     wb:=wbmessage
  378.     args:=wb.arglist
  379.     iconbase:=OpenLibrary('icon.library',37)
  380.  
  381.     /* Read the actual program icon, for tooltypes */
  382.  
  383.     IF ((args.lock>0) AND (args.name>0))
  384.     olddir := CurrentDir(args.lock)
  385.     ENDIF
  386.  
  387.     IF dobj:=GetDiskObject(args.name)
  388.       toolarray := dobj.tooltypes
  389.  
  390.       /* Read Tooltypes here */
  391.  
  392.       IF typtr:=FindToolType(toolarray,'DIR')
  393.         StrCopy(defdir,typtr,ALL)
  394.       ENDIF
  395.  
  396.       IF typtr:=FindToolType(toolarray,'CIV')
  397.         StrCopy(civdir,typtr,ALL)
  398.       ENDIF
  399.  
  400.       IF typtr:=FindToolType(toolarray,'FONTS')
  401.         StrCopy(fontdir,typtr,ALL)
  402.       ENDIF
  403.  
  404.       IF olddir THEN CurrentDir(olddir)
  405.         FreeDiskObject(dobj)
  406.       ENDIF
  407.  
  408.       /* Now read the first argument's lock */
  409.  
  410.       IF wb.numargs >= 2
  411.         IF (args[1].lock>0) AND (StrLen(args[1].name)>0)
  412.  
  413.           NameFromLock(args[1].lock,filename,256)
  414.           SetStr(filename,StrLen(filename))
  415.           IF filename[EstrLen(filename)-1]<>":"
  416.           StrAdd(filename,'/',ALL)
  417.         ENDIF
  418.         StrAdd(filename,args[1].name,ALL)
  419.       ELSE
  420.         request('You must select a FILE icon as\nan argument to Civmanager, or\nrun it without arguments.\n',quitstring,[])
  421.       ENDIF 
  422.     ELSE
  423.       filereq(defdir,filename)
  424.     ENDIF
  425.  
  426.     CloseLibrary(iconbase)
  427.   ENDIF
  428.  
  429.  
  430. EXCEPT
  431.   Raise(exception)
  432. ENDPROC
  433.  
  434. CHAR '$VER: Civmanager V1.3 ©1994 Paul Hickman',0